package com.miracle9.game.util;
import com.miracle9.game.service.vo.FishDeskVo;
import net.sf.json.JSONObject;
import org.apache.commons.lang.RandomStringUtils;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AliLiveUtil {
    /**
     * 计算md5
     * @param param
     * @return
     */
    public static String md5(String param) {
        if(param == null || param.length() == 0) {
            return null;
        }
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(param.getBytes());
            byte[] byteArray = md5.digest();

            BigInteger bigInt = new BigInteger(1, byteArray);
            // 参数16表示16进制
            String result = bigInt.toString(16);
            // 不足32位高位补零
            while(result.length() < 32) {
                result = "0" + result;
            }
            return result;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 生成推流地址
     * @param pushDomain 推流域名
     * @param pushKey 推流域名配置的鉴权Key
     * @param appName 推流AppName
     * @param streamName 推流StreamName
     * @param expireTime 过期时间（单位是秒）
     */
    public static String generate_push_url(String pushDomain,String pushKey,String appName,String streamName,long expireTime) {
        String pushUrl = "";
        //推流域名未开启鉴权功能的情况下
        if(pushKey=="") {
            pushUrl = "rtmp://"+pushDomain+"/"+appName+"/"+streamName;
        }else {
            long timeStamp = System.currentTimeMillis()/1000L + expireTime;
            String stringToMd5 = "/"+appName+"/"+streamName+"-"+Long.toString(timeStamp)+"-0-0-"+pushKey;
            String authKey = md5(stringToMd5);
            pushUrl = "rtmp://"+pushDomain+"/"+appName+"/"+streamName+"?auth_key="+Long.toString(timeStamp)+"-0-0-"+authKey;
        }
       return pushUrl;
    }

    public static String generate_push_url(String appName,String streamName) {
        String pushUrl = "";
        long expireTime = 360000L;
        String pushDomain = "push.bscgd.cn";
        String pushKey = "uY4pHR21WVhY33Y4";
        //推流域名未开启鉴权功能的情况下
        if(pushKey=="") {
            pushUrl = "rtmp://"+pushDomain+"/"+appName+"/"+streamName;
        }else {
            long timeStamp = System.currentTimeMillis()/1000L + expireTime;
            String stringToMd5 = "/"+appName+"/"+streamName+"-"+Long.toString(timeStamp)+"-0-0-"+pushKey;
            String authKey = md5(stringToMd5);
            pushUrl = "rtmp://"+pushDomain+"/"+appName+"/"+streamName+"?auth_key="+Long.toString(timeStamp)+"-0-0-"+authKey;
        }
        return pushUrl;
    }
    /**
     * 生成播放地址
     * @param appName 播放appName（同推流appName)
     * @param streamName 播放streamName，播放源流时，streamName 同推流streamName；播放转码流时，streamName 为推流streamName_{转码模板ID}
     */
    public static Map<String,Object> general_pull_url(String appName,String streamName) {
        String rtmpUrl = ""; //rtmp的拉流地址
        String hlsUrl = ""; //m3u8的拉流地址
        String flvUrl = ""; //flv的拉流地址
        Map<String,Object> map = new HashMap<>();
        long expireTime = 360000L;
        String pullDomain = "pull.bscgd.cn";
        String pullKey = "MWnvsKb27KUFTmkO";
        //播放域名未配置鉴权Key的情况下
        if(pullKey == "") {
            rtmpUrl = "rtmp://"+pullDomain+"/"+appName+"/"+streamName;
            hlsUrl = "http://"+pullDomain+"/"+appName+"/"+streamName+".m3u8";
            flvUrl = "http://"+pullDomain+"/"+appName+"/"+streamName+".flv";
        }else {
            long timeStamp = System.currentTimeMillis()/1000L + expireTime;
            String rtmpToMd5 = "/"+appName+"/"+streamName+"-"+Long.toString(timeStamp)+"-0-0-"+pullKey;
            String rtmpAuthKey = md5(rtmpToMd5);
            rtmpUrl = "rtmp://"+pullDomain+"/"+appName+"/"+streamName+"?auth_key="+Long.toString(timeStamp)+"-0-0-"+rtmpAuthKey;

            String hlsToMd5 = "/"+appName+"/"+streamName+".m3u8-"+Long.toString(timeStamp)+"-0-0-"+pullKey;
            String hlsAuthKey = md5(hlsToMd5);
            hlsUrl = "http://"+pullDomain+"/"+appName+"/"+streamName+".m3u8"+"?auth_key="+Long.toString(timeStamp)+"-0-0-"+hlsAuthKey;

            String flvToMd5 = "/"+appName+"/"+streamName+".flv-"+Long.toString(timeStamp)+"-0-0-"+pullKey;
            String flvAuthKey = md5(flvToMd5);
            flvUrl = "http://"+pullDomain+"/"+appName+"/"+streamName+".flv"+"?auth_key="+Long.toString(timeStamp)+"-0-0-"+flvAuthKey;
        }
        map.put("rtmpUrl",rtmpUrl);
        map.put("flvUrl",flvUrl);
        map.put("hlsUrl",hlsUrl);
        return map;
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //生成长度为5的随机字符串作为appName和streamName（字母和数字组合）
    /*    String appName = RandomStringUtils.randomAlphanumeric(5);
        String streamName = RandomStringUtils.randomAlphanumeric(5);

        long expireTime = 36000L;
        String pullDomain = "pull.bscgd.cn";
        String pullKey = "MWnvsKb27KUFTmkO";

        String pushDomain = "push.bscgd.cn";
        String pushKey = "uY4pHR21WVhY33Y4";
        System.out.println(general_pull_url( appName, streamName));
        System.out.println(generate_push_url(pushDomain, pushKey, appName, streamName, expireTime));*/


      /*  int s = 23^00^128^00^00^00^00^00^00^01^00^00^00^00^00^00^00^00^00^00;
        System.out.println(hexToInt("80"));*/

    //    byte[] data = {0x17 ,0x00, (byte)0x80,0x00,0x00 ,0x00 ,0x00 ,0x00, 0x00, 0x01, 0x00 ,0x00, 0x00,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00, 0x00};
       // String s = "17 00 80 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00".replace(" ", "");
    //    System.out.println(calculateChecksum(data));
 /*       System.out.println(hexToInt("71"));
        System.out.println(hexToInt("01"));
        System.out.println(hexToInt("01"));
        System.out.println(Integer.toHexString(115));*/
        Map<String, Integer> seats = new HashMap<>();
        for (int i = 1; i < 8+1; i++) {
            seats.put(i+"",0);
        }

        System.out.println(seats);
    }
    public static int calculateChecksum(byte[] data) {
        int checksum = 0;
        for (byte b : data) {
            checksum = checksum^b ;
        }
        return checksum;
    }

    public static int hexToInt(String hex) {
        int result = 0;
        int len = hex.length();
        for (int i = 0; i < len; i++) {
            char c = hex.charAt(i);
            int digit = Character.digit(c, 16);
            result = result * 16 + digit;
        }
        return result;
    }
    public static String makeChecksum(String data) {
        if (data == null || data.equals("")) {
            return "";
        }
        int total = 0;
        int len = data.length();
        int num = 0;
        while (num < len) {
            String s = data.substring(num, num + 2);
            // System.out.println(s);
            total += Integer.parseInt(s, 16);
            num = num + 2;
        }
        /**
         * 用256求余最大是255，即16进制的FF
         */
        int mod = total % 256;
        String hex = Integer.toHexString(mod);
        len = hex.length();
        // 如果不够校验位的长度，补0,这里用的是两位校验
        if (len < 2) {
            hex = "0" + hex;
        }
        return hex;
    }

}
